package org.jeecg.zhensi.purchase.service.impl;

import org.jeecg.zhensi.purchase.entity.AssetPurchaseOrder;
import org.jeecg.zhensi.purchase.entity.AssetPurchaseOrderZi;
import org.jeecg.zhensi.purchase.mapper.AssetPurchaseOrderZiMapper;
import org.jeecg.zhensi.purchase.mapper.AssetPurchaseOrderMapper;
import org.jeecg.zhensi.purchase.service.IAssetPurchaseOrderService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 资产采购订单
 * @Author: jeecg-boot
 * @Date:   2023-01-13
 * @Version: V1.0
 */
@Service
public class AssetPurchaseOrderServiceImpl extends ServiceImpl<AssetPurchaseOrderMapper, AssetPurchaseOrder> implements IAssetPurchaseOrderService {

	@Autowired
	private AssetPurchaseOrderMapper assetPurchaseOrderMapper;
	@Autowired
	private AssetPurchaseOrderZiMapper assetPurchaseOrderZiMapper;
	
	@Override
	@Transactional
	public void saveMain(AssetPurchaseOrder assetPurchaseOrder, List<AssetPurchaseOrderZi> assetPurchaseOrderZiList) {
		assetPurchaseOrderMapper.insert(assetPurchaseOrder);
		if(assetPurchaseOrderZiList!=null && assetPurchaseOrderZiList.size()>0) {
			for(AssetPurchaseOrderZi entity:assetPurchaseOrderZiList) {
				//外键设置
				entity.setAssetPurchaseOrderH(assetPurchaseOrder.getId());
				assetPurchaseOrderZiMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(AssetPurchaseOrder assetPurchaseOrder,List<AssetPurchaseOrderZi> assetPurchaseOrderZiList) {
		assetPurchaseOrderMapper.updateById(assetPurchaseOrder);
		
		//1.先删除子表数据
		assetPurchaseOrderZiMapper.deleteByMainId(assetPurchaseOrder.getId());
		
		//2.子表数据重新插入
		if(assetPurchaseOrderZiList!=null && assetPurchaseOrderZiList.size()>0) {
			for(AssetPurchaseOrderZi entity:assetPurchaseOrderZiList) {
				//外键设置
				entity.setAssetPurchaseOrderH(assetPurchaseOrder.getId());
				assetPurchaseOrderZiMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		assetPurchaseOrderZiMapper.deleteByMainId(id);
		assetPurchaseOrderMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			assetPurchaseOrderZiMapper.deleteByMainId(id.toString());
			assetPurchaseOrderMapper.deleteById(id);
		}
	}
	
}
